package com.spica.platform.feign.factory;

import com.spica.platform.base.config.BaseFeignConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class GenericFeignClientFactory<T> {
	private FeignClientBuilder feignClientBuilder;
	@Autowired
	private BaseFeignConfig.UserErrorDecoder userErrorDecoder;

	public GenericFeignClientFactory(ApplicationContext appContext) {
		this.feignClientBuilder = new FeignClientBuilder(appContext);
	}

	public T getFeignClient(final Class<T> type, String serviceId) {
		return this.feignClientBuilder.forType(type, serviceId).customize(item -> item.errorDecoder(userErrorDecoder)).decode404(true).build();
	}
}
